<?php
App::uses('AppModel', 'Model');
/**
 * Calendario Model
 *
 * @property Anuncio $Anuncio
 * @property Anuncio $Anuncio
 */
class Calendario extends AppModel {
/**
 * Primary key field
 *
 * @var string
 */
	public $primaryKey = 'id_calendario';
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'id_calendario' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'anuncio_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'dia_da_semana' => array(
			'date' => array(
				'rule' => array('date'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed



/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Anuncio' => array(
			'className' => 'Anuncio',
			'foreignKey' => 'anuncio_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	
	public function diasOcupados($anuncio){
		$calendario = $this->find('all', array('conditions'=>array('anuncio_id'=>$anuncio)));
		if(!empty($calendario)){
			foreach($calendario as $item){
				$data = explode('-',$item['Calendario']['dia_da_semana']);
				if($data[1] == 06)
					$dias[] = $data[2];
				else if($data[1] == 07)
					$dias[] = $data[2]+31;
			}
			return $dias;
		}
	}
	
	public function dataCompleta($dia){
		$dia = explode('-', $dia);
		return array('month'=>$dia[1],'day'=>$dia[0],'year'=>2014);
	}
	
	public function dataToBd($dia){
		$dia = explode('-', $dia);
		return '2014-'.$dia[1].'-'.$dia[0];
	}
	
	public function salvarDatas($dados){
		$adicionar = array();
		if(!empty($dados['dias_da_semana'])){
			foreach ($dados['dias_da_semana'] as $item){
				$adicionar[] = array('Calendario' =>array('anuncio_id'=>$dados['anuncio_id'], 'dia_da_semana'=>$this->dataCompleta($item)));
			}
			if($this->saveMany($adicionar)) return true; else return false;
		}
		return true;
	}
	
	public function remocao($dados){
		if(!empty($dados['remove_dias_da_semana']))
			foreach ($dados['remove_dias_da_semana'] as $item){
				$this->deleteAll(array('anuncio_id'=>$dados['anuncio_id'], 'dia_da_semana'=>$this->dataToBd($item)));
			}
		//return $return;
	}
}
